package com.jy.controller;


import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.user.param.UpdateAvatar;
import com.jy.common.model.user.param.UpdateUserBgDto;
import com.jy.common.tool.CommonTool;
import com.jy.common.tool.ToolForDateTime;
import com.jy.service.UserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author fzg
 * @since 2024-11-08
 */
@RestController
@RequestMapping("/user")
public class UserController {

    // 指定的保存图片文件的路径，这里假设是项目根目录下的"uploads"文件夹，可根据实际情况修改
    private static final String UPLOAD_PATH = System.getProperty("user.dir") + "/uploads";

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }


    /**
     * 获取用户信息（token）
     */
    @GetMapping("/getUserInfo")
    public ResponseWrapper getUserInfo(HttpServletRequest request) {
        return userService.getUserInfo(request);
    }

    /**
     * 用户上传照片
     */
    @PostMapping("/setting/upload-img")
    public ResponseWrapper uploadImg(@RequestParam("file") MultipartFile file) throws IOException {
        if(file.isEmpty()){
            return ResponseWrapper.markCustomError("图片文件为空");
        }
        // 文件夹新建为：upload_img/2024/11/11  这种格式的目录
        String currentDate = ToolForDateTime.LocalDateTimeToStringToDate(LocalDateTime.now());
        // 获取当前年份
        String year = currentDate.substring(0, 4);
        // 获取当前月份
        String month = currentDate.substring(5, 7);
        // 获取当前日期
        String day = currentDate.substring(8);
        // 图片的原始文件名
        String fileName = file.getOriginalFilename();
        // 文件存储路径
        String filePath = UPLOAD_PATH + "/img/" + year + "/" + month + "/" + day + "/" + fileName;
        // 创建父目录（如果不存在）
        File saveFile  = new File(filePath);
        File parentDir = saveFile .getParentFile();
        if (!parentDir.exists()) {
            parentDir.mkdirs();
        }
        file.transferTo(saveFile );
        return userService.uploadImg(filePath);
    }

    /**
     * 将本条动态aid添加到当前登录用户的user表的不喜欢里
     * @param request 前端请求带过来token
     * @param dynamicAid 当前这条动态的aid
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/disLikeThisDynamicApi")
    public ResponseWrapper disLikeThisDynamicApi(HttpServletRequest request, @RequestParam("dynamicAid") Integer dynamicAid) {
        return userService.disLikeThisDynamicApi(CommonTool.getUserAidByToken(request),dynamicAid);
    }


    /**
     * 调用后端把数据库里面的用户背景地址更新
     * @param request request
     * @param param 用户的背景URL
     * @return ResponseWrapper
     */
    @PostMapping("/updateUserBg")
    public ResponseWrapper updateUserBg(HttpServletRequest request, @RequestBody @Validated UpdateUserBgDto param){
        return userService.updateUserBg(CommonTool.getUserAidByToken(request),param);
    }


    /**
     * 更改头像
     * @param request request
     * @param param 参数
     * @return ResponseWrapper
     */
    @PostMapping("/updateAvatar")
    public ResponseWrapper updateAvatar(HttpServletRequest request, @RequestBody @Validated UpdateAvatar param){
        return userService.updateAvatar(CommonTool.getUserAidByToken(request),param);
    }


    /**
     * 我的信息页面获取用户所有信息
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getUserAllInfoInSetting")
    public ResponseWrapper getUserAllInfoInSetting(HttpServletRequest request) {
        return userService.getUserAllInfoInSetting(CommonTool.getUserAidByToken(request));
    }



    /**
     * 获取用户主页的全部信息
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getUserDetails")
    public ResponseWrapper getUserDetails(HttpServletRequest request, @RequestParam Integer userAid) {
        return userService.getUserDetails(CommonTool.getUserAidByToken(request),userAid);
    }


    /**
     * 获取用户的关注和粉丝列表
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getFollowsAndFans")
    public ResponseWrapper getFollowsAndFans(HttpServletRequest request, @RequestParam Integer userAid) {
        return userService.getFollowsAndFans(CommonTool.getUserAidByToken(request),userAid);
    }





    /**
     * 获取用户的关注和粉丝列表
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getMyBusinessCard")
    public ResponseWrapper getMyBusinessCard(HttpServletRequest request) {
        return userService.getMyBusinessCard(CommonTool.getUserAidByToken(request));
    }




    /**
     * 获取收藏列表
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getCollectList")
    public ResponseWrapper getCollectList(HttpServletRequest request, Integer userAid) {
        return userService.getCollectList(CommonTool.getUserAidByToken(request),userAid);
    }

    /**
     * 获取用户的帖子（笔记）列表
     * @param request 前端请求带过来token
     * @return 类型ResponseWrapper类
     */
    @GetMapping("/getMyPost")
    public ResponseWrapper getMyPost(HttpServletRequest request, Integer userAid) {
        return userService.getMyPost(CommonTool.getUserAidByToken(request),userAid);
    }


}
